package com.hzit.security.controller;

import com.hzit.security.entity.LoginUser;
import com.hzit.security.entity.User;
import com.hzit.security.service.IUserService;
import com.hzit.security.vo.UserVo;
import com.hzit.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;

	//1. 用户登录
	@PostMapping("/login")
	public R login(@RequestBody UserVo vo, HttpServletResponse response){

		R r = userService.login(vo);
		// response.addCookie(new Cookie("token",r.getData().toString()));
		return r;
	}

	//2. 访问用户列表
	@GetMapping("/list")
	@PreAuthorize("hasAuthority('user:touserlistxx')")
	public R list(){
		List<User> userList = userService.list();
		LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		System.out.println("登录名:  " + user.getUsername());
		return R.ok(userList);
	}
}
